package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketRegion;
import com.cskaoyan.bean.PO.MarketRegionExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @author:刘杰
 * @create: 2022-08-14 16:00
 */
@Service
public class AdminRegionServiceImpl implements AdminRegionService{

    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public CommonData list() {
        MarketRegionExample marketRegionExample1 = new MarketRegionExample();
        MarketRegionExample.Criteria criteria1 = marketRegionExample1.createCriteria();
        criteria1.andPidEqualTo(0)
                .andTypeEqualTo((byte) 1);

        List<MarketRegion> marketRegions1 = marketRegionMapper.selectByExample(marketRegionExample1);

        //对省份和城市遍历
        for (MarketRegion marketRegion : marketRegions1) {
            MarketRegionExample marketRegionExample2 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria2 = marketRegionExample2.createCriteria();
            criteria2.andPidEqualTo(marketRegion.getId())
                    .andTypeEqualTo((byte) 2);
            List<MarketRegion> marketRegions2 = marketRegionMapper.selectByExample(marketRegionExample2);
            for (MarketRegion region : marketRegions2) {
                MarketRegionExample marketRegionExample3 = new MarketRegionExample();
                MarketRegionExample.Criteria criteria3 = marketRegionExample3.createCriteria();
                criteria3.andPidEqualTo(region.getId())
                        .andTypeEqualTo((byte) 3);
                List<MarketRegion> marketRegions3 = marketRegionMapper.selectByExample(marketRegionExample3);
                region.setChildren(marketRegions3);
            }
            marketRegion.setChildren(marketRegions2);
        }

        PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions1);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }
}
